/*
============================================================================
 Name        : BlockJBM.cpp
 Author      : \x{738b}\x{658c}
 Version     :
 Copyright   : \x{76ca}\x{76df}\x{8f6f}\x{4ef6}
 Description : CBlockJBM implementation
============================================================================
*/

#include "BlockJBM.h"
#include "eStockappui.h"
#include "Global.h"
#include "Field.h"
#include "Goods.h"
#include "BlockBargain.h"
#include "BlockPicHis.h"
#include "BlockPicCur.h"
#include <eStock_L2hy.rsg>

CBlockJBM::CBlockJBM(CBaseBlock* bsBack):
CBlockGoods(bsBack),
m_nStart(0)
{
    iOffset = 0;
    iHeight = m_nLineHeight;
}

CBlockJBM::~CBlockJBM()
{
	if(m_pstrFieldName)
		delete m_pstrFieldName, m_pstrFieldName = NULL;
	
	if (m_vFHSP)
        delete m_vFHSP, m_vFHSP = NULL;

    if (iFlickable != NULL)
        delete iFlickable, iFlickable = NULL;
}

CBlockJBM* CBlockJBM::NewLC(CBaseBlock* bsBack)
{
	CBlockJBM* self = new (ELeave)CBlockJBM(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockJBM* CBlockJBM::NewL(CBaseBlock* bsBack)
{
	CBlockJBM* self=CBlockJBM::NewLC(bsBack);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockJBM::ConstructL()
{
	CBlockGoods::ConstructL();
	m_pstrFieldName = new (ELeave)CDesCArraySeg(11);
	m_bCanSetGoods = ETrue;
	CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_MENU);

	for(TInt i=0; i<13; i++)
	{
		m_pxJBM[i].m_cMul = 0;
		m_pxJBM[i].m_nBase = 0;
	}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_DATE);	//\x{65e5}\x{671f}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_MGSY);	//\x{6bcf}\x{80a1}\x{6536}\x{76ca}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_MGJZC);	//\x{6bcf}\x{80a1}\x{51c0}\x{8d44}\x{4ea7}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_JZCSY);	//\x{51c0}\x{8d44}\x{4ea7}\x{6536}\x{76ca}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_PROFITS);	//\x{5e02}\x{76c8}\x{7387}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_PPBV);	//\x{5e02}\x{51c0}\x{7387}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_TOTALEQUITY);	//\x{603b}\x{80a1}\x{672c}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_SJLT);	//\x{5b9e}\x{9645}\x{6d41}\x{901a}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_XSLT);	//\x{9650}\x{552e}\x{6d41}\x{901a}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_LTSZ);	//\x{6d41}\x{901a}\x{5e02}\x{503c}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_TOTALVALUE);	//\x{603b}\x{5e02}\x{503c}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_GDHS);	//\x{80a1}\x{4e1c}\x{6237}\x{6570}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_HJCG);	//\x{6237}\x{5747}\x{6301}\x{80a1}
	
	m_vFHSP = new (ELeave)CDesCArrayFlat(1);
	m_nHelpType = MRTSHelp;

    iFlickable = CFlickable::NewL();
    iFlickable->SetAcceptMouseClick(this);
    iFlickable->SetThreshold(m_nLineHeight);
}

void CBlockJBM::AddTabs()
{
	TBuf<8> label;
	CGlobal::GetDesFromID(label, LOC_BUF_MENU_DPTJ);
	label.Copy(label);
	iTabbar->AppendTab(label, KTabDPTJ);
	CGlobal::GetDesFromID(label, LOC_BUF_MENU_PROMPT2);
	label.Copy(label);
	iTabbar->AppendTab(label, KTabMRTS);
	CGlobal::GetDesFromID(label, LOC_BUF_MENU_FINANCIAL2);
	label.Copy(label);
	iTabbar->AppendTab(label, KTabGSCW);
}

void CBlockJBM::OnDraw(CFbsBitGc& g)const
{
	TInt nField = m_pstrFieldName->Count();
	TInt nFHSP = m_vFHSP->Count();
	TInt nSize = nField+nFHSP;

	TField field;
	field.m_nGoodsID = m_goods.m_nGoodsID;
	field.m_nClose = m_goods.m_nClose;

	g.UseFont(m_pGlobal->g_font);

	if (m_bOutofTest)
	{
		g.SetPenColor(m_pGlobal->g_rgbText);
		g.DrawText(m_strOutofTest, TPoint(0, m_rect.iTl.iY+m_pGlobal->g_nFontHeight));
		return;
	}
	
	g.SetPenColor(m_pGlobal->g_rgbHighlight);

    iStartItem = iOffset / iHeight;
    TInt y = iListRect.iTl.iY + iStartItem * iHeight - iOffset;
    if (iOffset <= 0)
    {
        iStartItem = 0;
        y = iListRect.iTl.iY;
    }
    iEndItem = iStartItem + iListRect.Height() / iHeight + 1;
    if (iEndItem > nField - 1)
        iEndItem = nField - 1;
    
    g.SetClippingRect(iListRect);
	for (TInt i = iStartItem; i < iEndItem; i++)
	{
		TInt nPos = iStartItem+i;
		if (nPos>=nSize)
			break;

		TInt nY = /*m_rect.iTl.iY + m_nLineHeight * i*/y + m_pGlobal->m_nSpaceUp;
		if (nPos<nField)
		{
			g.SetPenColor(m_pGlobal->g_rgbText);
			g.DrawText((*m_pstrFieldName)[nPos], TPoint(0, nY+m_pGlobal->g_nFontHeight));

			field.m_nDateJBM = m_pxJBM[0].GetRawData();

			field.m_sID = (TInt16)(1000+nPos);
			field.m_lValue = m_pxJBM[nPos].GetValue();
			if (nPos>=15)
			{
				field.Draw(g, m_rect.Width()/2+m_pGlobal->g_nFontHeight, nY,RIGHT);

				field.m_sID++;
				field.m_lValue = m_goods.GetValue(field.m_sID);
				field.Draw(g, m_rect.Width(), nY, RIGHT);
			}
			else
				field.Draw(g, m_rect.Width(), nY,RIGHT);
		}
		else
		{
			g.SetPenColor(m_pGlobal->g_rgbVolume);
			g.DrawText((*m_vFHSP)[nPos-nField], TPoint(0, nY+m_pGlobal->g_nFontHeight));
		}
		
		y += iHeight;
	}
	g.CancelClippingRect();
}

void CBlockJBM::OnKeyDown(TInt keyCode)
{
	if (keyCode == m_pGlobal->g_nKeyUp || keyCode == m_pGlobal->g_nKeyDown)
    {
        TInt nLine = m_pstrFieldName->Count()+m_vFHSP->Count();
        TInt offset = iOffset;
        if (keyCode == m_pGlobal->g_nKeyUp)
        {
            offset -= iHeight;
            if (offset < 0)
                iTabbar->SetFocus(ETrue, m_pGlobal->g_nKeyUp);
        }
        else if (keyCode == m_pGlobal->g_nKeyDown)
        {
            if (offset >= iHeight * nLine - iListRect.Height())
                iToolbar->SetFocus(ETrue, m_pGlobal->g_nKeyDown);
            else
                offset += iHeight;
        }
        SetScrollOffset(TPoint(0, offset));
        DrawNow();
    }
	else if (keyCode==m_pGlobal->g_nKeySelect)//\x{70b9}ok\x{8fdb}\x{77ed}\x{671f}\x{64cd}\x{76d8}\x{7ebf}
	{
		CBlockPicHis* bph = CBlockPicHis::NewL(m_pBlockBack);
		bph->SetGoods(m_goods);
		bph->m_nKLineType = 0;
		bph->m_bDataType = 102;
		GetContainer()->AddBlock(bph);
		return;
	}
	else
		CBlockGoods::OnKeyDown(keyCode);
}

void CBlockJBM::OnInput(const TDesC8& strInput)
{
	if (strInput.CompareF(_L8("01")) == 0)
	{
		StopSocket();

		CBlockBargain* bar = CBlockBargain::NewL(m_pBlockBack);	//attention
		bar->SetGoods(m_goods);
		GetContainer()->AddBlock(bar);
		return;
	}
	else if (strInput.CompareF(_L8("02")) == 0)
	{
		StopSocket();

		CBlockPicHis* ph = CBlockPicHis::NewL(m_pBlockBack);	//attention
		ph->SetGoods(m_goods);
		GetContainer()->AddBlock(ph);
		return;
	}
	else if (strInput.CompareF(_L8("05")) == 0)
	{
		StopSocket();

		CBlockPicCur* pc = CBlockPicCur::NewL(m_pBlockBack);	//attention
		pc->SetGoods(m_goods);
		GetContainer()->AddBlock(pc);
		return;
	}
	else if((strInput.CompareF(_L8("06")) == 0))
	{
		StopSocket();
		
		CBlockPicHis* blockph = CBlockPicHis::NewL(m_pBlockBack);
        blockph->m_nKLineType = 1;
        blockph->SetGoods(m_goods);
		GetContainer()->AddBlock(blockph);
		return;
	}
	CBlock::OnInput(strInput);
}


TInt32 CBlockJBM::GetMenuItem()
{
	return 6;
}

void CBlockJBM::InitData()
{
	m_bOutofTest = EFalse;
	m_bSocketed = EFalse;
	TInt nField = m_pstrFieldName->Count();
	for (TInt i = 0; i<nField; i++)
			m_pxJBM[i].SetRawData(0);
	m_vFHSP->Reset();
	m_nStart = 0;
	SetScrollBar(0, nField+m_vFHSP->Count());
}

TBool CBlockJBM::ReadData(const TDesC8& aData)
{
	TPtrC8 ptr(aData);

	TInt32 nGoodsID = CGlobal::ReadInt32L(ptr);
	if (nGoodsID==m_goods.m_nGoodsID)
	{

		TInt8 bStatus = CGlobal::ReadInt8L(ptr);
		if ((bStatus&1)!=0)
		{
			HBufC* buf = CGlobal::ReadStringL(ptr);
			m_goods.m_strName.Copy(buf->Des());
			delete buf;
			m_pGlobal->AddGoods2ZJ(m_goods.m_nGoodsID, m_goods.m_strName);

			TInt nField = m_pstrFieldName->Count();
			for (TInt i = 0; i<nField; i++)
				m_pxJBM[i].SetRawData(CGlobal::ReadInt32L(ptr));


			TBuf<10> buffh;
			CGlobal::GetDesFromID(buffh,LOC_BUF_STR_FH);
			HBufC* strFHSP = CGlobal::ReadStringL(ptr);
			HBufC* bufstr = HBufC::NewL(buffh.Length()+strFHSP->Length());
			bufstr->Des().Copy(buffh);
			bufstr->Des().Append(strFHSP->Des());
			delete strFHSP;

			CreateTipArray(bufstr->Des(), *m_vFHSP);
			delete bufstr;

			SetScrollBar(0, m_pstrFieldName->Count()+m_vFHSP->Count());

			m_bExit = ETrue;    //\x{672c}\x{7c7b}\x{53ea}\x{6536}\x{4e00}\x{904d}\x{6570}\x{636e}
		}
	}
	return ETrue;
}

void CBlockJBM::WriteData(TDes8& aBuf)
{
	m_bOutofTest = EFalse;
	CGlobal::WriteInt32L(aBuf,m_goods.m_nGoodsID);
}

TInt CBlockJBM::GetDataLength()
{
	return 4;
}

TInt16 CBlockJBM::GetDataType()
{
	if (m_bSocketed)
		return RequestDataType_None;
	else
		return RequestDataType_JBM;
}

void CBlockJBM::ChangeScrollPos(TInt nPos)
{
	if (m_nStart!=nPos)
	{
		m_nStart = nPos;
		DrawNow();
	}
}

void CBlockJBM::SizeChanged()
{
	CBlockGoods::SizeChanged();
	iListRect = m_rect;
	TInt nField = m_pstrFieldName->Count();
	SetScrollBar(0, nField+m_vFHSP->Count());
	iTitlebar->FocusEnabled(ETrue);
	iTabbar->SetSelectedId(KTabGSCW);
}

TPoint CBlockJBM::ScrollOffset() const
{
    return TPoint(0, iOffset);
}

void CBlockJBM::SetScrollOffset(const TPoint& aOffset)
{
    TInt y = aOffset.iY;
    if (y != iOffset)
    {
        iOffset = Max(0, Min(iHeight * (m_pstrFieldName->Count() + m_vFHSP->Count()) - iListRect.Height(), y));
        DrawNow();

//        if (m_bShowScroll)
//        {
//            
//            if (iStartItem == 0)
//                m_sbVert->SetPos(iStartItem);
//            else if (iEndItem == iTotalLines - 1)
//                m_sbVert->SetPos(iEndItem);
//            else
//                m_sbVert->SetPos((iStartItem + iEndItem) / 2);
//        }
    }
}

void CBlockJBM::OnMouseDown(const TPoint& aPt)
{
    iFlickable->HandleMousePress(aPt);
}

void CBlockJBM::OnMouseMove(const TPoint& aPt)
{
    iFlickable->HandleMouseMove(aPt);
}

void CBlockJBM::OnMouseUp(const TPoint& aPt)
{
    iFlickable->HandleMouseRelease(aPt);
}
